package com.controller;

import com.pojo.R;
import com.pojo.Student;
import com.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/student")
public class StudentController {
    @Autowired
    private StudentService studentService;

    @GetMapping("/allStudent")
    public R list() {
        List<Student> list = studentService.queryAllStudent();
        return R.ok(list);
    }

    @PostMapping("/addStudent")
    public R addClass(@RequestBody Student student) {
        studentService.addStudent(student);
        return R.ok();
    }

    @PostMapping("/del/{id}")
    public R deleteClass(@PathVariable("id") long id) {
        studentService.deleteStudentById(id);
        return R.ok();
    }

    @PostMapping("/updateStudent")
    public R update(@RequestBody Student student) {
        studentService.updateStudent(student);
        student = studentService.queryById(student.getStudentId());
        return R.ok(student);
    }

    @GetMapping("/getStudentById")
    public R getClassById(@RequestParam("id") long id) {
        Student student = studentService.queryById(id);
        return R.ok(student);
    }
}
